package  com.erastep.util
{
	import com.erastep.json.JSON;
	
	import mx.utils.ObjectUtil;
	import mx.utils.StringUtil;
	

	/**
	 * @date 2011.4.7
	 * @author jarvis
	 * */
	public class StringUtil
	{
		/**
		 * 字符串替换方法
		 * @param str main String  原始字符
		 * @param rel delete str   被替换的字符
		 * @param repl replace str 替换字符
		 * @return 替换成功的字符
		 * */
		public static function replace(str:String,rel:String,repl:String):String{
			if(str){
				if(trim(str) != ""){
					str = str.split(rel).join(repl);
		        	return str;	
				}else{
					return "";
				}
			}else{
				return "";
			}
		}
		/**
		 * 字符串Json转换成对象
		 * @param str jsonString
		 * @return Json to object
		 * */
		public static function JsonDecode(str:String,strict:Boolean = true):*{
			//str = replace(str,"'","\"");            //暂且注释此方法，便于后期修改.
			var json:*;
			try
			{
				json = JSON.decode(str,strict);
			} 
			catch(error:Error) 
			{
				LogUtil.print("后台返回数据格式错误!  "+str);
				json = null;
			}
			return json;
		}
		/**
		 * 对象转换成字符串Json
		 * @param obj
		 * @return str
		 * */
		public static function objectToJson(obj:Object):String{
			var str:String = JSON.encode(obj);
			return str;
		}
		/**
		 * 消息名首字母大写转换
		 * @param str
		 * @return Method name
		 * */
		public static function getMethodName(str:String):String{
			if(str != null && str.length > 0){
				str = mx.utils.StringUtil.trim(str);
				str = str.substr(0,1).toUpperCase()+str.substring(1);
			}
			str = CommonConstants.METHODHEAD + str;
			return str;
		}
		
		/**
		 * 获取Xform中的Style并解析成对象
		 * @param style
		 * @return object Object
		 * */
		public static function stringToStyle(str:String):Object{
			var obj:Object = new Object();
			try{
				var arr:Array = str.split(";");
				for each(var sty:String in arr){
					obj[mx.utils.StringUtil.trim(sty.split(":")[0])] = sty.split(":")[1];
				}
			}catch(e:Error){
				trace("resolver style error!");
			}
			return obj;
		}
		
		/**
		 * 清除两端空字符
		 * @param String str
		 * */
		public static function trim(str:String):String{
			return mx.utils.StringUtil.trim(str);
		}
		
		/**
		 * 截取DataStore的id
		 * @param String str
		 * @return id
		 * */
		public static function getInstanceId(str:String):String{
			var start:int = str.indexOf("('");
			var end:int = str.lastIndexOf("')");
			str = str.substr(start+2,end-start-2);
			return str;
		}
		
		/**
		 * 日期对比函数
		 * @param start 起始日期
		 * @param end 结束日期
		 * @return start > end 返回  -1,start == end 返回 0,start < end 返回 1
		 * */
		public static function compareDate(start:Date,end:Date):int{
			if(Number(start) > Number(end)){
				return -1;
			}else if(Number(start) == Number(end)){
				return 0;
			}else if(Number(start) < Number(end)){
				return 1;
			}
			return -2;
		}
		
		/**
		 * 替换Json对象中的特殊日期类型
		 * @param json String
		 * @return json String
		 * */
		public static function replaceJsonTime(json:String):String{
			var rxp:RegExp=/\d{4}-\d{2}-\d{2}\s\d{2}\|\|\d{2}\|\|\d{2}.\d{1}/g;  
			var arr:Array = json.match(rxp);
			var temp:String;
			for each(var str:String in arr){
				temp = str;
				temp = replace(temp,"||",":");
				json = replace(json,str,temp);
			}
			return json;
		}
		
		/**
		 * 获取对象属性
		 * */
		//返回所传参数所有的属性
		public static function getProperties(source:Object):Array
		{
			if(source){
				var obj:Object=ObjectUtil.getClassInfo(source);
				return obj["properties"] as Array;
			}else{
				return new Array();
			}
		}
		/**
		 * 检查对象中是否已经存在此属性了
		 * */
		public static function checkProperties(source:Object,proper:String):String{
			var p:String;
			var arrp:Array = getProperties(source);
			var c:String = proper.toLocaleLowerCase();
			for each(var q:QName in arrp){
				if(c==q.localName.toLocaleLowerCase()){
					p = q.localName;
					break;
				}
			}
			return p;
		}
	}
}